
import requests
from lxml import etree

#获取链家所有城市及链接，组成字典  城市为key， 链接为value
def get_lianjia_all():
    result = []
    url = "https://www.lianjia.com/city/"
    # //*[@class="city_list"]//li/a
    response = requests.get(url)
    html = etree.HTML(response.text)
    city_list = html.xpath('//*[@class="city_list"]//li/a')
    if city_list:
        for city in city_list:
            href = city.xpath('./@href')

            city_name = city.xpath('string(.)')
            if href and city_name:
                result.append({
                    "city":city_name,
                    "href":href[0]
                })
    return result

res = get_lianjia_all()
print(res)